package com.example.student.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.constants.RdsConstants;
import com.example.common.domain.pojo.ClassTeacher;
import com.example.common.domain.pojo.Course;
import com.example.common.domain.pojo.CourseTeacher;
import com.example.common.utils.CacheClient;
import com.example.student.mapper.ClassTeacherMapper;
import com.example.student.mapper.CourseMapper;
import com.example.student.mapper.CourseTeacherMapper;
import com.example.student.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private CacheClient cacheClient;
    @Autowired
    private CourseTeacherMapper courseTeacherMapper;
    @Autowired
    private ClassTeacherMapper classTeacherMapper;
    @Autowired
    private CourseMapper courseMapper;


    @Override
    public Course getCourseByIdWithLogicalExpire(String id) {
        // 使用逻辑过期策略获取教师信息
        return cacheClient.queryWithLogicalExpire(
                RdsConstants.COURSE_INFO,
                id,
                Course.class,
                this::getById, // 使用MyBatis-Plus的基础方法
                RdsConstants.CACHE_NULL_TTL,
                TimeUnit.MINUTES,
                false
        );
    }

    @Override
    public List<Course> getCourseByClassId(String classId) {
        // 1. 通过班级ID查询ClassTeacher表，获取教师ID列表
        LambdaQueryWrapper<ClassTeacher> classTeacherQuery = new LambdaQueryWrapper<>();
        classTeacherQuery.eq(ClassTeacher::getClassId, classId);
        List<ClassTeacher> classTeachers = classTeacherMapper.selectList(classTeacherQuery);

        // 提取教师ID列表
        List<String> teacherIds = classTeachers.stream()
                .map(ClassTeacher::getTeacherId)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());

        // 2. 如果教师ID列表为空，直接返回空列表
        if (teacherIds.isEmpty()) {
            return Collections.emptyList();
        }
        // 3. 通过教师ID列表查询CourseTeacher表，获取课程ID列表
        LambdaQueryWrapper<CourseTeacher> courseTeacherQuery = new LambdaQueryWrapper<>();
        courseTeacherQuery.in(CourseTeacher::getTeacherId, teacherIds);
        List<CourseTeacher> courseTeachers = courseTeacherMapper.selectList(courseTeacherQuery);
        // 提取课程ID列表
        List<String> courseIds = courseTeachers.stream()
                .map(CourseTeacher::getCourseId)
                .filter(Objects::nonNull)
                .collect(Collectors.toList());

        // 4. 如果课程ID列表为空，直接返回空列表
        if (courseIds.isEmpty()) {
            return Collections.emptyList();
        }
        // 5. 通过课程ID列表查询Course表，获取最终课程列表
        LambdaQueryWrapper<Course> courseQuery = new LambdaQueryWrapper<>();
        courseQuery.in(Course::getId, courseIds);
        return courseMapper.selectList(courseQuery);
    }


    @Override
    @Transactional
    public Course getCourseByTeacherId(String teacherId) {
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getTeacherId, teacherId);
        CourseTeacher courseTeacher = courseTeacherMapper.selectOne(queryWrapper);
        if (courseTeacher == null) {
            return null;
        }
        return getCourseByIdWithLogicalExpire(courseTeacher.getCourseId());
    }


}
